package com.jiuzhe.commercial.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.jiuzhe.commercial.R;


public class CustomLoadingDialog extends Dialog {
    private Context context = null;
    private static CustomLoadingDialog customProgressDialog = null;

    public CustomLoadingDialog(Context context) {
        super(context);
        this.context = context;
    }

    public CustomLoadingDialog(Context context, int theme) {
        super(context, theme);
    }

    public static CustomLoadingDialog createDialog(Context context) {
        customProgressDialog = new CustomLoadingDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.cusdom_dialog);
       // customProgressDialog.setCanceledOnTouchOutside(false); //点击消
        //customProgressDialog.show();
       // customProgressDialog.setCancelable(false);//dialog弹出后会点击屏幕或物理返回键，dialog不消失
        Window window = customProgressDialog.getWindow();
        window.getAttributes().gravity = Gravity.CENTER;
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = context.getResources().getDisplayMetrics().widthPixels/5*4;
        window.setAttributes(lp);
        return customProgressDialog;
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }

        ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loadingImageView);
        AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
        animationDrawable.start();
    }



    /**
     * 和activity的onDestroy()方法同步
     */
    public void onDestroy() {
     if (customProgressDialog != null){
         customProgressDialog = null;
     }
    }
}